package alo;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @ClassName test7
 * @Description TODO
 * @Author Fanxc
 * @DATE 2021/4/15 19:06
 * @Version 1.0
 */
public class test7 {

        class Solution {
            public ListNode mergeKLists(ListNode[] lists) {
                if (lists == null || lists.length == 0) return null;
                PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, new Comparator<ListNode>() {
                    @Override
                    public int compare(ListNode o1, ListNode o2) {
                        if (o1.val < o2.val) return -1;
                        else if (o1.val == o2.val) return 0;
                        else return 1;
                    }
                });
                ListNode dummy = new ListNode(0);
                ListNode p = dummy;
                for (ListNode node : lists) {
                    if (node != null) queue.add(node);
                }
                while (!queue.isEmpty()) {
                    p.next = queue.poll();
                    p = p.next;
                    if (p.next != null) queue.add(p.next);
                }
                return dummy.next;
            }
            public class ListNode {
                int val;
                ListNode next;
                ListNode(int x) { val = x; }
            }
        }
}
